<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>从零开始搭建一个主流项目框架（一）—简单的框架 | 海晨忆的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  
  
  <meta name="description" content="目的&amp;emsp;&amp;emsp;首先先说出，最终的目的是现在主流的MVP+RxJava+Retrofit+OkHttp框架。让大家心里有底 &amp;emsp;&amp;emsp;开发工具Android Studio3.0，还在用eclipse的同鞋，强烈推荐你跨出这一步，你会发现一个新的世界。android studio都出来这么久了，你还在远古时代做开发，说句不好听的，你完全与时代脱轨，你不适合做开发（纯属个人观">
<meta property="og:type" content="article">
<meta property="og:title" content="从零开始搭建一个主流项目框架（一）—简单的框架">
<meta property="og:url" content="https://haichenyi.com/2018/02/23/从零开始搭建一个主流项目框架（一）—简单的框架/index.html">
<meta property="og:site_name" content="海晨忆的博客">
<meta property="og:description" content="目的&amp;emsp;&amp;emsp;首先先说出，最终的目的是现在主流的MVP+RxJava+Retrofit+OkHttp框架。让大家心里有底 &amp;emsp;&amp;emsp;开发工具Android Studio3.0，还在用eclipse的同鞋，强烈推荐你跨出这一步，你会发现一个新的世界。android studio都出来这么久了，你还在远古时代做开发，说句不好听的，你完全与时代脱轨，你不适合做开发（纯属个人观">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://haichenyi.com/uploads/article/2018-02-23/清单文件.png">
<meta property="og:image" content="https://haichenyi.com/uploads/article/2018-02-23/项目结构.png">
<meta property="og:image" content="https://haichenyi.com/uploads/article/2018-02-23/style.png">
<meta property="og:updated_time" content="2019-09-18T09:09:52.715Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="从零开始搭建一个主流项目框架（一）—简单的框架">
<meta name="twitter:description" content="目的&amp;emsp;&amp;emsp;首先先说出，最终的目的是现在主流的MVP+RxJava+Retrofit+OkHttp框架。让大家心里有底 &amp;emsp;&amp;emsp;开发工具Android Studio3.0，还在用eclipse的同鞋，强烈推荐你跨出这一步，你会发现一个新的世界。android studio都出来这么久了，你还在远古时代做开发，说句不好听的，你完全与时代脱轨，你不适合做开发（纯属个人观">
<meta name="twitter:image" content="https://haichenyi.com/uploads/article/2018-02-23/清单文件.png">
  
    <link rel="alternate" href="/atom.xml" title="海晨忆的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/uploads/artistic_image/head.jpg">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
  <link rel="stylesheet" href="/css/highlight.css">
  <script>
  let antiquityStorage = window.sessionStorage.getItem('antiquitySessionStorage');
  if (antiquityStorage == '' || antiquityStorage == null) {
    var antiquityLoader = '<div id="loaderbox"><div class="loader"><div class="load-roll"><div class="load-top"></div><div class="load-right"></div><div class="load-bottom"></div></div></div></div>';
    document.write(antiquityLoader);
    document.body.style.overflow = 'hidden'
  }
  </script>
</head>

<body>
  <div id="fullpage" class="mobile-nav-right">
    
      <div id="wrapper" style="background-image: url(/uploads/artistic_image/bg.jpg)" title="背景图片来自网络">
    
    
      <header id="header">
  <div id="nav-toggle" class="nav-toggle"></div>
  <div class="head-box global-width">
    <nav class="nav-box nav-right">
      
        <a class="nav-item" href="/" title
        
        >首页</a>
      
        <a class="nav-item" href="/archives" title
        
        >归档</a>
      
        <a class="nav-item" href="/about" title
        
        >简历</a>
      
    </nav>
  </div>
</header>
      <div id="middlecontent" title class="global-width sidebar-left">
        <section id="main"><article id="post-从零开始搭建一个主流项目框架（一）—简单的框架" class="article global-container article-type-post" itemscope itemprop="blogPost">
  
    <header class="article-header">
      
  
    <h1 class="article-title" itemprop="name">
      从零开始搭建一个主流项目框架（一）—简单的框架
    </h1>
  

    </header>
  
  <div class="article-meta">
    <a href="/2018/02/23/从零开始搭建一个主流项目框架（一）—简单的框架/" class="article-date">
  <time datetime="2018-02-23T12:30:11.000Z" itemprop="datePublished">2018-02-23</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Android-框架/">Android -框架</a>
  </div>

    
  </div>
  
    <span id="busuanzi_container_page_pv">
      本文总阅读量<span id="busuanzi_value_page_pv"></span>次
    </span>
  

  <div class="article-inner">
    
    <div class="article-content article-content-cloud doorframe mac" itemprop="articleBody">
      
        <h3 id="目的"><a href="#目的" class="headerlink" title="目的"></a>目的</h3><p>&emsp;&emsp;首先先说出，最终的目的是现在主流的MVP+RxJava+Retrofit+OkHttp框架。让大家心里有底</p>
<p>&emsp;&emsp;开发工具Android Studio3.0，还在用eclipse的同鞋，强烈推荐你跨出这一步，你会发现一个新的世界。android studio都出来这么久了，你还在远古时代做开发，说句不好听的，你完全与时代脱轨，你不适合做开发（纯属个人观点）</p>
<a id="more"></a>
<p>&emsp;&emsp;本篇就只有三部分，第一部分就是新建一个Application，第二部分就是BaseActivity，第三部分就是BaseFragment</p>
<h3 id="Application"><a href="#Application" class="headerlink" title="Application"></a>Application</h3><p>&emsp;&emsp;首先你得有application类，去初始化应用只用初始化一次的内容，继承Application，然后在清单文件里面注册。</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject;</span><br><span class="line"></span><br><span class="line">import android.app.Application;</span><br><span class="line"></span><br><span class="line">import com.squareup.leakcanary.LeakCanary;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc:</span><br><span class="line"> */</span><br><span class="line">public class MyApplication extends Application &#123;</span><br><span class="line">  private static MyApplication instance;</span><br><span class="line"></span><br><span class="line">  public MyApplication getInstance() &#123;</span><br><span class="line">    return instance;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  private void setInstance(MyApplication instance) &#123;</span><br><span class="line">    MyApplication.instance = instance;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void onCreate() &#123;</span><br><span class="line">    super.onCreate();</span><br><span class="line">    setInstance(this);</span><br><span class="line">    initLeakCanary();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 初始化内存检测工具</span><br><span class="line">   */</span><br><span class="line">  private void initLeakCanary() &#123;</span><br><span class="line">    if (LeakCanary.isInAnalyzerProcess(this)) &#123;</span><br><span class="line">      return;</span><br><span class="line">    &#125;</span><br><span class="line">    LeakCanary.install(this);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;如上代码，我这里就初始化了一个全局application单例对象，还初始化square公司出品的一个内存检测工具，用于检测你项目中内存泄漏情况。便于你优化项目。</p>
<p><img src="/uploads/article/2018-02-23/清单文件.png" alt="清单文件.png"></p>
<p>&emsp;&emsp;如上图所示，这个就是清单文件，在application结点下面，添加name标签，内容就是你创建的application的名字。这里你还需要添加两个内存检测的依赖。</p>
<p><img src="/uploads/article/2018-02-23/项目结构.png" alt="项目结构.png"></p>
<p>&emsp;&emsp;如上图所示，首先把你的项目结构视图切换到Project，打开你的app目录下的build.gradle文件，在dependencies结点下面（只要是添加开源库都是在该结点下面，后面就不说了），添加如下两行代码：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">releaseImplementation &apos;com.squareup.leakcanary:leakcanary-android-no-op:1.5.4&apos;</span><br><span class="line">debugImplementation &apos;com.squareup.leakcanary:leakcanary-android:1.5.4&apos;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;最后的1.5.4是版本号，你可以在github上面搜索<a href="https://github.com/square/leakcanary" target="_blank" rel="noopener">leakcanary</a>，找最新的版本</p>
<h3 id="BaseActivity"><a href="#BaseActivity" class="headerlink" title="BaseActivity"></a>BaseActivity</h3><p>&emsp;&emsp;创建基类BaseActivity，也就是所有Activity的父类。还有一个基类的接口BaseView，BaseActivity继承刚才添加的依赖的SupportActivity类，实现BaseView接口，并且实现点击事件的接口（选择实现，你要是不乐意在基类里面写，你可以在你自己的子类里面重新实现一遍也是可以的）。代码如下：每个方法注释写的很清楚，就不用一一解释了</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.base;</span><br><span class="line"></span><br><span class="line">import android.app.AlertDialog;</span><br><span class="line">import android.graphics.Color;</span><br><span class="line">import android.graphics.drawable.ColorDrawable;</span><br><span class="line">import android.os.Bundle;</span><br><span class="line">import android.support.annotation.Nullable;</span><br><span class="line">import android.view.Window;</span><br><span class="line">import android.widget.ProgressBar;</span><br><span class="line"></span><br><span class="line">import com.haichenyi.myproject.utils.ToastUtils;</span><br><span class="line"></span><br><span class="line">import me.yokeyword.fragmentation.SupportActivity;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc:</span><br><span class="line"> */</span><br><span class="line">public abstract class BaseActivity extends SupportActivity implements BaseView &#123;</span><br><span class="line">  private AlertDialog loadingDialog;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  protected void onCreate(@Nullable Bundle savedInstanceState) &#123;</span><br><span class="line">    super.onCreate(savedInstanceState);</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * Toast 提示用户</span><br><span class="line">   * @param msg 提示内容String</span><br><span class="line">   */</span><br><span class="line">  @Override</span><br><span class="line">  public void showTipMsg(String msg) &#123;</span><br><span class="line">    ToastUtils.showTipMsg(msg);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * Toast 提示用户</span><br><span class="line">   * @param msg 提示内容res目录下面的String的int值</span><br><span class="line">   */</span><br><span class="line">  @Override</span><br><span class="line">  public void showTipMsg(int msg) &#123;</span><br><span class="line">    ToastUtils.showTipMsg(msg);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 网络请求的时候显示正在加载的对话框</span><br><span class="line">   */</span><br><span class="line">  @Override</span><br><span class="line">  public void showLoading() &#123;</span><br><span class="line">    if (null == loadingDialog) &#123;</span><br><span class="line">      loadingDialog = new AlertDialog.Builder(this).setView(new ProgressBar(this)).create();</span><br><span class="line">      loadingDialog.setCanceledOnTouchOutside(false);</span><br><span class="line">      Window window = loadingDialog.getWindow();</span><br><span class="line">      if (null != window) &#123;</span><br><span class="line">        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    if (!loadingDialog.isShowing()) &#123;</span><br><span class="line">      loadingDialog.show();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 网络请求完成时隐藏加载对话框</span><br><span class="line">   */</span><br><span class="line">  @Override</span><br><span class="line">  public void hideLoading() &#123;</span><br><span class="line">    if (null != loadingDialog) &#123;</span><br><span class="line">      if (loadingDialog.isShowing()) &#123;</span><br><span class="line">        loadingDialog.dismiss();</span><br><span class="line">      &#125;</span><br><span class="line">      loadingDialog = null;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void invalidToken() &#123;</span><br><span class="line">    //用于检测你当前用户的token是否有效，无效就返回登录界面，具体的业务逻辑你自己实现</span><br><span class="line">    //如果需要做到实时检测，推荐用socket长连接，每隔10秒发送一个验证当前登录用户token是否过期的请求</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * Finish当前页面，最好实现onBackPressedSupport()，这个方法会有一个退栈操作，</span><br><span class="line">   * 开源框架实现的，我们不用管</span><br><span class="line">   */</span><br><span class="line">  @Override</span><br><span class="line">  public void myFinish() &#123;</span><br><span class="line">    onBackPressedSupport();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void onBackPressedSupport() &#123;</span><br><span class="line">    super.onBackPressedSupport();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;上面是目前BaseActivity代码，注释写的很清楚，你会发现BaseView你并没有，下面我给出BaseView的代码</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.base;</span><br><span class="line"></span><br><span class="line">import android.support.annotation.StringRes;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc:</span><br><span class="line"> */</span><br><span class="line">public interface BaseView &#123;</span><br><span class="line">  void showTipMsg(String msg);</span><br><span class="line"></span><br><span class="line">  void showTipMsg(@StringRes int msg);</span><br><span class="line"></span><br><span class="line">  void showLoading();</span><br><span class="line"></span><br><span class="line">  void hideLoading();</span><br><span class="line"></span><br><span class="line">  void invalidToken();</span><br><span class="line"></span><br><span class="line">  void myFinish();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;BaseView就是一个接口，是所有V层的基类，代码很简单，Toast方法，显示隐藏加载的对话框方法，检验token是否过期的方法，finish当前页面的方法。什么？Toast方法你没有，下面我贴出来我的Toast的工具类</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Author: 海晨忆.</span><br><span class="line"> * Date: 2017/12/21</span><br><span class="line"> * Desc: 实时更新的Toast工具类</span><br><span class="line"> */</span><br><span class="line">public final class ToastUtils &#123;</span><br><span class="line">  private static Toast toast;</span><br><span class="line"></span><br><span class="line">  private ToastUtils() &#123;</span><br><span class="line">    throw new RuntimeException(&quot;工具类不允许创建对象&quot;);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @SuppressWarnings(&quot;all&quot;)</span><br><span class="line">  private static void init() &#123;</span><br><span class="line">    if (toast == null) &#123;</span><br><span class="line">      toast = Toast.makeText(MyApplication.getInstance(), &quot;&quot;, Toast.LENGTH_SHORT);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public static void showTipMsg(String msg) &#123;</span><br><span class="line">    if (null == toast) &#123;</span><br><span class="line">      init();</span><br><span class="line">    &#125;</span><br><span class="line">    toast.setText(msg);</span><br><span class="line">    toast.show();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public static void showTipMsg(@StringRes int msg) &#123;</span><br><span class="line">    if (null == toast) &#123;</span><br><span class="line">      init();</span><br><span class="line">    &#125;</span><br><span class="line">    toast.setText(msg);</span><br><span class="line">    toast.show();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;上面我贴出了三个类，这里我要说明的是，我又创建了两个package，一个是base，一个是utils，我把BaseActivity,BaseView,MyApplication放在base包下面，Toast的工具类放在utils包下面</p>
<p>&emsp;&emsp;再就是添加一些常用的东西了，这里我没有用黄油刀，用过一段时间之后，感觉他的每个控件都是全局的，有点占内存，就放弃了。我下面贴出BaseActivity新增的伪代码：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">   * 保存当前activity对象，在OnCreate里面添加，记得在OnDestroy里面移除</span><br><span class="line">   * 有什么用呢？</span><br><span class="line">   * 比方说有一个需求，让你在任意位置弹出对话框，弹对话框又需要一个context对象，这个时候，</span><br><span class="line">   * 你就只用传当前list的最上层的activity对象就可以了</span><br><span class="line">   * 当然还有其他需求</span><br><span class="line">   */</span><br><span class="line">  public static List&lt;BaseActivity&gt; activities = new ArrayList&lt;&gt;();</span><br><span class="line">  private Toolbar toolbar;</span><br><span class="line">  private TextView tvToolbarTitle;</span><br><span class="line">  private TextView tvToolbarRight;</span><br><span class="line">  private TextView tvBack;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  protected void onCreate(@Nullable Bundle savedInstanceState) &#123;</span><br><span class="line">    super.onCreate(savedInstanceState);</span><br><span class="line">    activities.add(this);</span><br><span class="line">    //强制竖屏(不强制加)</span><br><span class="line">    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);</span><br><span class="line">    int layoutId = getLayoutId(savedInstanceState);</span><br><span class="line">    View inflate = getLayoutInflater().inflate(R.layout.activity_base, toolbar, false);</span><br><span class="line">    LinearLayout rootLinearLayout = inflate.findViewById(R.id.ll_layout_base_activity);</span><br><span class="line">    //没有布局的时候传0</span><br><span class="line">    if (0 == layoutId) &#123;</span><br><span class="line">      setContentView(rootLinearLayout);</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">      View rootView = getLayoutInflater().inflate(layoutId, rootLinearLayout, true);</span><br><span class="line">      setContentView(rootView);</span><br><span class="line">    &#125;</span><br><span class="line">    stateBar();</span><br><span class="line">    initView();</span><br><span class="line">    initData();</span><br><span class="line">    setOnClick(R.id.tv_back_base_activity);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 设置点击事件.</span><br><span class="line">   *</span><br><span class="line">   * @param ids 被点击View的ID</span><br><span class="line">   * @return &#123;@link BaseActivity&#125;</span><br><span class="line">   */</span><br><span class="line">  public BaseActivity setOnClick(@IdRes int... ids) &#123;</span><br><span class="line">    View view;</span><br><span class="line">    for (int id : ids) &#123;</span><br><span class="line">      view = findViewById(id);</span><br><span class="line">      if (null != view) &#123;</span><br><span class="line">        view.setOnClickListener(this);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 设置点击事件.</span><br><span class="line">   *</span><br><span class="line">   * @param views 被点击View</span><br><span class="line">   * @return &#123;@link BaseActivity&#125;</span><br><span class="line">   */</span><br><span class="line">  public BaseActivity setOnClick(View... views) &#123;</span><br><span class="line">    for (View view : views) &#123;</span><br><span class="line">      view.setOnClickListener(this);</span><br><span class="line">    &#125;</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 获取当前布局对象</span><br><span class="line">   *</span><br><span class="line">   * @param savedInstanceState 这个是当前activity保存的数据，最常见的就是横竖屏切换的时候，</span><br><span class="line">   *                           数据丢失问题</span><br><span class="line">   * @return 当前布局的int值</span><br><span class="line">   */</span><br><span class="line">  protected abstract int getLayoutId(Bundle savedInstanceState);</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  protected void onDestroy() &#123;</span><br><span class="line">    activities.remove(this);</span><br><span class="line">    super.onDestroy();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  protected void initData() &#123;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  protected void initView() &#123;</span><br><span class="line">    toolbar = findViewById(R.id.toolbar_base_activity);</span><br><span class="line">    tvToolbarTitle = findViewById(R.id.tv_title_base_activity);</span><br><span class="line">    tvToolbarRight = findViewById(R.id.tv_right_base_activity);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 设置状态栏背景颜色，不能改变状态栏内容的颜色</span><br><span class="line">   */</span><br><span class="line">  private void stateBar() &#123;</span><br><span class="line">    if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.KITKAT) &#123;</span><br><span class="line">      getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);</span><br><span class="line">    &#125;</span><br><span class="line">    SystemBarTintManager tintManager = new SystemBarTintManager(this);</span><br><span class="line">    tintManager.setStatusBarTintEnabled(true);</span><br><span class="line">    tintManager.setNavigationBarTintEnabled(true);</span><br><span class="line">    tintManager.setTintColor(Color.parseColor(&quot;#000000&quot;));</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void onClick(View v) &#123;</span><br><span class="line">    switch (v.getId()) &#123;</span><br><span class="line">      case R.id.tv_back_base_activity:</span><br><span class="line">        onBackPressedSupport();</span><br><span class="line">        break;</span><br><span class="line">      default:</span><br><span class="line">        break;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;这里我需要说明的是，新增了一个开源框架，就是设置状态栏背景颜色的<a href="https://github.com/jgilfelt/SystemBarTint" target="_blank" rel="noopener">systembartint</a>。</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">implementation &apos;com.readystatesoftware.systembartint:systembartint:1.0.3&apos;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;再就是设置activity标题内容，左边，右边的内容，左边右边可能是文字，也可能是图片。所以，我在用的时候，都是用的TextView，ImageView，不能设置文字。方法如下：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line">public BaseActivity setTitles(CharSequence title) &#123;</span><br><span class="line">    tvToolbarTitle.setText(title);</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 初始化toolbar的内容</span><br><span class="line">   * @param isShowToolbar 是否显示toolbar</span><br><span class="line">   * @param isShowBack 是否显示左边的TextView</span><br><span class="line">   * @param isShowMore 是否显示右边的TextView</span><br><span class="line">   * @return 当前activity对象，可以连点</span><br><span class="line">   */</span><br><span class="line">  protected BaseActivity initToolbar(boolean isShowToolbar, boolean isShowBack,</span><br><span class="line">                                     boolean isShowMore) &#123;</span><br><span class="line">    setSupportActionBar(toolbar);</span><br><span class="line">    ActionBar actionBar = getSupportActionBar();</span><br><span class="line">    if (null != actionBar) &#123;</span><br><span class="line">      if (isShowToolbar) &#123;</span><br><span class="line">        actionBar.show();</span><br><span class="line">        tvBack = findViewById(R.id.tv_back_base_activity);</span><br><span class="line">        TextView textView = findViewById(R.id.tv_right_base_activity);</span><br><span class="line">        if (null != tvBack &amp;&amp; null != textView) &#123;</span><br><span class="line">          tvBack.setVisibility(isShowBack ? View.VISIBLE : View.INVISIBLE);</span><br><span class="line">          textView.setVisibility(isShowMore ? View.VISIBLE : View.INVISIBLE);</span><br><span class="line">        &#125;</span><br><span class="line">      &#125; else &#123;</span><br><span class="line">        actionBar.hide();</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public BaseActivity setToolbarBack(int colorId) &#123;</span><br><span class="line">    toolbar.setBackgroundColor(getResources().getColor(colorId));</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @SuppressWarnings(&quot;unused&quot;)</span><br><span class="line">  public BaseActivity setMyTitle(String title) &#123;</span><br><span class="line">    tvToolbarTitle.setText(title);</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public BaseActivity setMyTitle(@StringRes int stringId) &#123;</span><br><span class="line">    tvToolbarTitle.setText(stringId);</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public void setMoreTitle(String moreTitle) &#123;</span><br><span class="line">    tvToolbarRight.setText(moreTitle);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  public BaseActivity setMoreTitle(@StringRes int stringId) &#123;</span><br><span class="line">    tvToolbarRight.setText(stringId);</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 设置左边内容.</span><br><span class="line">   *</span><br><span class="line">   * @param leftTitle 内容</span><br><span class="line">   * @return &#123;@link BaseActivity&#125;</span><br><span class="line">   */</span><br><span class="line">  public BaseActivity setLeftTitle(String leftTitle) &#123;</span><br><span class="line">    if (tvBack != null) &#123;</span><br><span class="line">      tvBack.setBackground(null);</span><br><span class="line">      tvBack.setText(leftTitle);</span><br><span class="line">    &#125;</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 设置左边内容.</span><br><span class="line">   *</span><br><span class="line">   * @param leftTitle 内容</span><br><span class="line">   */</span><br><span class="line">  public void setLeftTitle(@StringRes int leftTitle) &#123;</span><br><span class="line">    if (tvBack != null) &#123;</span><br><span class="line">      tvBack.setBackground(null);</span><br><span class="line">      tvBack.setText(leftTitle);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @SuppressWarnings(&quot;unused&quot;)</span><br><span class="line">  protected BaseActivity setMoreBackground(int resId) &#123;</span><br><span class="line">    tvToolbarRight.setBackgroundResource(resId);</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;可以看到上面的方法返回值都是BaseActivity，这样做的目的就只有一个，可以连点，写一个方法之后，可以接着点写下一个方法，不用写一个方法就要加分号，就换一行写下一个方法。</p>
<p>&emsp;&emsp;还要加一句，在你的app主题里面添加两个item，也就是你的res目录下面的style：</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;item name=&quot;windowActionBar&quot;&gt;false&lt;/item&gt;</span><br><span class="line">&lt;item name=&quot;windowNoTitle&quot;&gt;true&lt;/item&gt;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;我这里贴出我目前的style的图片</p>
<p><img src="/uploads/article/2018-02-23/style.png" alt="style.png"></p>
<p>&emsp;&emsp;下面有一个LineHorizontal样式，就是你toolbar下面的那个横线</p>
<h3 id="BaseFragment"><a href="#BaseFragment" class="headerlink" title="BaseFragment"></a>BaseFragment</h3><p>&emsp;&emsp;BaseFragment跟BaseActivity的逻辑是差不多的，我这里就贴出代码</p>
<div class="highlight-box"autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" contenteditable="true"data-rel="PLAIN"><figure class="iseeu highlight /plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br></pre></td><td class="code"><pre><span class="line">package com.haichenyi.myproject.base;</span><br><span class="line"></span><br><span class="line">import android.os.Bundle;</span><br><span class="line">import android.support.annotation.IdRes;</span><br><span class="line">import android.support.annotation.NonNull;</span><br><span class="line">import android.support.annotation.Nullable;</span><br><span class="line">import android.view.LayoutInflater;</span><br><span class="line">import android.view.View;</span><br><span class="line">import android.view.ViewGroup;</span><br><span class="line"></span><br><span class="line">import com.haichenyi.myproject.utils.ToastUtils;</span><br><span class="line"></span><br><span class="line">import me.yokeyword.fragmentation.SupportFragment;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Author: 海晨忆</span><br><span class="line"> * Date: 2018/2/23</span><br><span class="line"> * Desc:</span><br><span class="line"> */</span><br><span class="line">public abstract class BaseFragment extends SupportFragment implements BaseView,</span><br><span class="line">    View.OnClickListener &#123;</span><br><span class="line">  protected boolean isInit;</span><br><span class="line">  private View rootView;</span><br><span class="line"></span><br><span class="line">  @Nullable</span><br><span class="line">  @Override</span><br><span class="line">  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,</span><br><span class="line">                           @Nullable Bundle savedInstanceState) &#123;</span><br><span class="line">    int layoutRes = layoutRes();</span><br><span class="line">    if (0 != layoutRes) &#123;</span><br><span class="line">      return inflater.inflate(layoutRes, null);</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">      return super.onCreateView(inflater, container, savedInstanceState);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) &#123;</span><br><span class="line">    super.onViewCreated(view, savedInstanceState);</span><br><span class="line">    rootView = view;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void onLazyInitView(@Nullable Bundle savedInstanceState) &#123;</span><br><span class="line">    super.onLazyInitView(savedInstanceState);</span><br><span class="line">    isInit = true;</span><br><span class="line">    init();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  protected &lt;T extends View&gt; T findViewById(@IdRes int id) &#123;</span><br><span class="line">    return rootView.findViewById(id);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 设置点击事件.</span><br><span class="line">   *</span><br><span class="line">   * @param ids 被点击View的ID</span><br><span class="line">   * @return &#123;@link BaseFragment&#125;</span><br><span class="line">   */</span><br><span class="line">  public BaseFragment setOnClick(@IdRes int... ids) &#123;</span><br><span class="line">    for (int id : ids) &#123;</span><br><span class="line">      rootView.findViewById(id).setOnClickListener(this);</span><br><span class="line">    &#125;</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 设置点击事件.</span><br><span class="line">   *</span><br><span class="line">   * @param views 被点击View的ID</span><br><span class="line">   * @return &#123;@link BaseFragment&#125;</span><br><span class="line">   */</span><br><span class="line">  public BaseFragment setOnClick(View... views) &#123;</span><br><span class="line">    for (View view : views) &#123;</span><br><span class="line">      view.setOnClickListener(this);</span><br><span class="line">    &#125;</span><br><span class="line">    return this;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  protected abstract void init();</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void onDestroy() &#123;</span><br><span class="line">    rootView = null;</span><br><span class="line">    super.onDestroy();</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  protected abstract int layoutRes();</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void showTipMsg(String msg) &#123;</span><br><span class="line">    ToastUtils.showTipMsg(msg);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void showTipMsg(int msg) &#123;</span><br><span class="line">    ToastUtils.showTipMsg(msg);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void showLoading() &#123;</span><br><span class="line">    BaseActivity activity = (BaseActivity) getActivity();</span><br><span class="line">    /*if (activity instanceof BaseMvpActivity) &#123;</span><br><span class="line">      activity.showLoading();</span><br><span class="line">    &#125;*/</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void hideLoading() &#123;</span><br><span class="line">    BaseActivity activity = (BaseActivity) getActivity();</span><br><span class="line">    /*if (activity instanceof BaseMvpActivity) &#123;</span><br><span class="line">      activity.hideLoading();</span><br><span class="line">    &#125;*/</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void invalidToken() &#123;</span><br><span class="line">    BaseActivity activity = (BaseActivity) getActivity();</span><br><span class="line">    /*if (activity instanceof BaseMvpActivity) &#123;</span><br><span class="line">      activity.invalidToken();</span><br><span class="line">    &#125;*/</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void onClick(View v) &#123;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  @Override</span><br><span class="line">  public void myFinish() &#123;</span><br><span class="line">    onBackPressedSupport();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>
<p>&emsp;&emsp;两者在布局抽象方法里面有一点区别，Activity的传了Boundle参数，Fragment没有传，因为Fragment可以通过getArguments()方法获取到这个对象，而Activity不能获取到。</p>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p>&emsp;&emsp;到此，一个简单的项目框架就出来了，目前还是框架的第一步，是一个雏形，还不包括MVP，dagger等等，下一篇就加上MVP，我这个人有个好习惯，就是喜欢写注释，我注释写的很清楚，是干什么用的，我也衷心的希望，你能写好注释。</p>
<p><a href="http://download.csdn.net/download/qq_27634797/10256144" target="_blank" rel="noopener">项目链接</a></p>

      
    </div>
    
      <footer class="article-footer">
        完
      </footer>
    
  </div>
  
    
<nav id="article-nav">
  <div class="article-nav-block">
    
      <a href="/2018/02/23/从零开始搭建一个主流项目框架（二）—MVP-dagger2/" id="article-nav-newer" class="article-nav-link-wrap">
        <strong class="article-nav-caption"></strong>
        <div class="article-nav-title">
          
            从零开始搭建一个主流项目框架（二）—MVP+dagger2
          
        </div>
      </a>
    
  </div>
  <div class="article-nav-block">
    
      <a href="/2018/02/22/MVC、MVP、MVVM比较/" id="article-nav-older" class="article-nav-link-wrap">
        <div class="article-nav-title">MVC、MVP、MVVM比较</div>
        <strong class="article-nav-caption"></strong>
      </a>
    
  </div>
</nav>

    
  
  
</article>
</section>
        <aside id="sidebar">
  
    <div class="widget-box">
  <div class="avatar-box avatar-item">
    <img class="avatar" src="/uploads/artistic_image/head.jpg" title="头像来自网络"></img>
    <h3 class="avatar-name">
      
        海晨忆
      
    </h3>
    <p class="avatar-slogan">
      先谋生，再谋爱。人间值得，未来可期。
    </p>
  </div>
</div>


  
    
  <div class="widget-box">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Android-Socket/">Android -Socket</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-加密算法/">Android -加密算法</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-常用功能/">Android -常用功能</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-框架/">Android -框架</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-源码解析/">Android -源码解析</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-自定义view/">Android -自定义view</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Android-设计模式/">Android -设计模式</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/JAVA-并发/">JAVA -并发</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java-SpringBoot/">Java -SpringBoot</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Tomcat/">Tomcat</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/kotlin/">kotlin</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/数据库-JDBC/">数据库 -JDBC</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/数据库-MySQL/">数据库 -MySQL</a></li></ul>
    </div>
  </div>


  
    

  
    
  
    
  <div class="widget-box">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/11/">November 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/10/">October 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/09/">September 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/08/">August 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">July 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">June 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">October 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/05/">May 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">March 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/">February 2018</a></li></ul>
    </div>
  </div>

  
    
  <div class="widget-box">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/07/27/JAVA—公平锁，非公平锁，悲观锁，乐观锁，死锁/">JAVA—公平锁，非公平锁，悲观锁，乐观锁，死锁</a>
          </li>
        
          <li>
            <a href="/2021/07/19/JAVA—线程同步器AQS/">JAVA—线程同步器AQS</a>
          </li>
        
          <li>
            <a href="/2021/07/07/Android模拟点击/">Android模拟点击</a>
          </li>
        
          <li>
            <a href="/2019/11/27/SpringBoot系列-消息-RabbitMQ-（二十）/">SpringBoot系列-消息(RabbitMQ)（二十）</a>
          </li>
        
          <li>
            <a href="/2019/11/13/SpringBoot系列-Docker（十九）/">SpringBoot系列-Docker（十九）</a>
          </li>
        
      </ul>
    </div>
  </div>

  
      <div class="widget-box">
    <h3 class="widget-title">友情链接</h3>
    <div class="widget">
      
        <a class="hrf" style="display: block;" href="https://github.com/haichenyi" title target='_blank'
        >Github</a>
      
        <a class="hrf" style="display: block;" href="https://www.jianshu.com/u/6077ee440c37" title target='_blank'
        >简书</a>
      
        <a class="hrf" style="display: block;" href="https://blog.csdn.net/qq_27634797" title target='_blank'
        >CSDN</a>
      
    </div>
  </div>

  
</aside>
      </div>
      <footer id="footer">
  <div class="foot-box footers global-width">
    &copy;2017-2021 海晨忆 &nbsp;&nbsp;
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span id="busuanzi_container_site_pv">阁下是第<span id="busuanzi_value_site_pv"></span>个访客</span>
  </div>
</footer>
      <script src="https://code.jquery.com/jquery-2.0.3.min.js"></script>
<script>
if (!window.jQuery) {
var script = document.createElement('script');
script.src = "/js/jquery-2.0.3.min.js";
document.body.write(script);
}
</script>

  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



    </div>
    <nav id="mobile-nav" class="mobile-nav-box">
  <div class="mobile-nav-img mobile-nav-top"></div>
  
    <a href="/" class="mobile-nav-link">首页</a>
  
    <a href="/archives" class="mobile-nav-link">归档</a>
  
    <a href="/about" class="mobile-nav-link">简历</a>
  
  <div class="mobile-nav-img  mobile-nav-bottom"></div>
</nav>    
  </div>
</body>
</html>